//
//  MVPremiereViewController.m
//  CapriceMusic
//
//  Created by 蒋恩浩 on 16/1/6.
//  Copyright © 2016年 MYX. All rights reserved.
//

#import "MVPremiereViewController.h"
#import "JEHNetWorkTool.h"
#import "MVPremierePageModel.h"
#import "MVPremiereTableViewCell.h"


@interface MVPremiereViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *modelArray;

@property (nonatomic, strong) NSArray *titleArray;

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) NSArray *jumpArray;

@property (nonatomic, assign) NSInteger offset;

@property (nonatomic, copy) NSString *jumpStr;

@end

@implementation MVPremiereViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"MV首播";
    self.modelArray = [NSMutableArray arrayWithCapacity:0];
    
    self.titleArray = @[@"韩国", @"日本",@"全部",@"内地",@"港台",@"欧美",@"韩国", @"日本",@"全部",@"内地",@"港台",@"欧美",@"韩国", @"日本",@"全部",@"内地",@"港台",@"欧美"];
    self.jumpArray = @[@"KR",@"JP",@"ALL",@"ML",@"HT",@"US",@"KR",@"JP",@"ALL",@"ML",@"HT",@"US",@"KR",@"JP",@"ALL",@"ML",@"HT",@"US"];

    BaseNavigationView *nav = [[BaseNavigationView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30) withArray:self.titleArray withJumpArray:self.jumpArray];
    nav.delegate = self;
    [self.view addSubview:nav];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 30, self.view.frame.size.width, self.view.frame.size.height - 94) style:UITableViewStyleGrouped];
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.backgroundColor = [UIColor blackColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle  = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[MVPremiereTableViewCell class] forCellReuseIdentifier:@"MVPremiereTableViewCellIdentifier"];
    [self.view addSubview:self.tableView];
    [self getData:@"ALL"];
    self.jumpStr = @"ALL";
    [self addHeaderRefresh];
    [self addFootRefresh];
}



- (void)sendRegion:(NSString *)region {
    self.jumpStr = region;
    [self getData:region];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MVPremiereTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MVPremiereTableViewCellIdentifier"];
    if (self.modelArray.count) {
        cell.mvpageModel = self.modelArray[indexPath.row];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.view.frame.size.height / 3.5;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    DetailViewController *detailVC = [[DetailViewController alloc] init];
    MVPremierePageModel *mvpageModel = self.modelArray[indexPath.row];
    detailVC.appendingId = mvpageModel.ids;
    detailVC.type = @"VIDEO";
    [self.navigationController pushViewController:detailVC animated:YES];
}

- (void)getData:(NSString *)str {
    [self.modelArray removeAllObjects];
    NSString *string = @"&offset=0&size=20";
   [JEHNetWorkTool getWithUrl:[[MVPremierePageGETURL stringByAppendingString:str] stringByAppendingString:string] parameter:nil httpHeader:@{@"App-Id":@"10201029",@"Device-Id":@"5fc617ae1220d0969b23eaab4c8f02b5",@"Device-V":@"QW5kcm9pZF80LjQuNF8xMDgwKjE5MjBfMTEwMDA5MDAw"} responseType:ResponseTypeJSON success:^(id result) {
       NSArray *arr = result[@"videos"];
       for (NSDictionary *dic in arr) {
           MVPremierePageModel *mvpageModel = [[MVPremierePageModel alloc] initWithDictionary:dic];
           [self.modelArray addObject:mvpageModel];
       }
       [_tableView reloadData];
       [_tableView setContentOffset:CGPointMake(0, 0) animated:NO];
   } fail:^(NSError *error) {
       
   }];
}

- (void)addHeaderRefresh {
    __weak typeof(self) weakSelf = self;
    [self.tableView addHeaderWithCallback:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [weakSelf.modelArray removeAllObjects];
            [weakSelf getData:weakSelf.jumpStr];
            [weakSelf.tableView headerEndRefreshing];
        });
    }];
}

- (void)addFootRefresh {
    __weak typeof(self) weakSelf = self;
    [self.tableView addFooterWithCallback:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            weakSelf.offset += 20;
            NSString *string = [NSString stringWithFormat:@"&offset=%ld&size=20", weakSelf.offset];
            [JEHNetWorkTool getWithUrl:[[MVPremierePageGETURL stringByAppendingString:weakSelf.jumpStr] stringByAppendingString:string] parameter:nil httpHeader:@{@"App-Id":@"10201029",@"Device-Id":@"5fc617ae1220d0969b23eaab4c8f02b5",@"Device-V":@"QW5kcm9pZF80LjQuNF8xMDgwKjE5MjBfMTEwMDA5MDAw"} responseType:ResponseTypeJSON success:^(id result) {
                NSArray *arr = result[@"videos"];
                for (NSDictionary *dic in arr) {
                    MVPremierePageModel *mvpageModel = [[MVPremierePageModel alloc] initWithDictionary:dic];
                    [weakSelf.modelArray addObject:mvpageModel];
                }
                dispatch_async(dispatch_get_main_queue(), ^{
                    [weakSelf.tableView reloadData];
                    [weakSelf.tableView footerEndRefreshing];
                });
            } fail:^(NSError *error) {
                
            }];
        });
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
